/*
 * RegisterForm
 */
package com.casestudy1.view;

import com.casestudy1.main.MainMidlet;
import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.Layout;
import com.sun.lwuit.spinner.Spinner;
import com.sun.lwuit.table.TableLayout;

/**
 * Create register form view
 *
 * @author Windsea
 */
public class RegisterForm extends View {

    String date1 = "28/2/1991";
    private Label lbticker, lbun, lbpw, lbrepw, lbgdr, lbbrdy;
    private TextField txtUsername, txtPassword, txtRepassword, txtDate;
    private RadioButton male;
    private RadioButton female;
    private Button btnReg;
    private Command cmBack, cmReg;
    private Dialog dialogDate;
    Spinner dateSpinner, monthSpinner, yearSpinner;
    LoginForm lgform;
    public boolean isLocal = true;
    String username,password;

    public RegisterForm(final MainMidlet midlet) {
        super(midlet);
        setTitle("Register");
        setTransitionOutAnimator(CommonTransitions.createSlide(
                CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
////////////////////////////////////////////////////////////////////////////////
//                                    View                                    //    
////////////////////////////////////////////////////////////////////////////////
//~Container1-------------------------------------------------------------------
        Layout layout = new TableLayout(3, 2);
        Container container1 = new Container(layout);
        //~InitComponent--------------------------------------------------------
        lbticker = new Label("Fill out the information below...");
        lbun = new Label("Username");
        lbpw = new Label("Password");
        lbrepw = new Label("Rewrite");
        txtUsername = new TextField();
        txtPassword = new TextField();
        txtPassword.setConstraint(TextField.PASSWORD);
        txtRepassword = new TextField();
        txtRepassword.setConstraint(TextField.PASSWORD);
        //~add component to containter1-----------------------------------------
//        container1.addComponent(lbticker);
        container1.addComponent(lbun);
        container1.addComponent(txtUsername);

        container1.addComponent(lbpw);
        container1.addComponent(txtPassword);
        container1.addComponent(lbrepw);

        container1.addComponent(txtRepassword);
//~Container2-------------------------------------------------------------------
        Container container2 = new Container(new TableLayout(1, 3));
        //~InitComponent--------------------------------------------------------
        lbgdr = new Label("Gender:");
        lbbrdy = new Label("Birthday:");
        male = new RadioButton("Male");
        female = new RadioButton("Female");
        final ButtonGroup btgender = new ButtonGroup();
        dateSpinner = Spinner.create(1, 31, 1, 1);
        monthSpinner = Spinner.create(1, 12, 1, 1);
        yearSpinner = Spinner.create(1900, 2007, 1991, 1);
        btnReg = new Button("Register");
        //~add component to containter2-----------------------------------------
        //add to btgender
        btgender.add(male);
        btgender.add(female);
        container1.addComponent(lbgdr);
        container1.addComponent(male);
        container1.addComponent(new Label(""));
        container1.addComponent(female);


        container2.addComponent(dateSpinner);
        container2.addComponent(monthSpinner);
        container2.addComponent(yearSpinner);

//~EventHandler-----------------------------------------------------------------
        btnReg.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                username = txtUsername.getText();
                password = txtPassword.getText();
                String repass = txtRepassword.getText();

                if (username.equals("")) {
                    Dialog.show("Error",
                            "Username not null",
                            "OK",
                            null);
                } else if (password.equals("")) {
                    Dialog.show("Error",
                            "Password not null",
                            "OK",
                            null);
                } else if (!repass.equals(password)) {
                    Dialog.show("Error",
                            "Password and Repassword not same",
                            "OK",
                            null);
                } else if (!btgender.isSelected()) {
                    Dialog.show("Error",
                            "Choose your sex",
                            "OK",
                            null);
                } else {
                    if (midlet.getLoginForm().getIsLocal()) {
                        midlet.setUser(username, password);
                        Dialog.show("Successful",
                                "Registered",
                                "OK",
                                null);
                        midlet.showLoginForm();
                    } else {
                        Dialog.show("Remote",
                                "Connecting to server",
                                "OK",
                                null);
                    }
                }
            }
        });
        cmBack = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                midlet.showLoginForm();
            }
        };
        setBackCommand(cmBack);
        cmReg = new Command("Register") {
            public void actionPerformed(ActionEvent evt) {
                Dialog.show(
                        "Successful",
                        "Register successful",
                        Dialog.TYPE_INFO,
                        null,
                        "OK :)",
                        null);
            }
        };
//addCommand--------------------------------------------------------------------
        addCommand(cmBack);
//        addCommand(cmReg);
//addComponent
        addComponent(lbticker);
        addComponent(container1);
        addComponent(lbbrdy);
        addComponent(container2);
        addComponent(btnReg);
    }
}
